package com.htthorizon.powersupply.controller;

import cn.hutool.core.date.DatePattern;
import com.htthorizon.powersupply.dto.TgLowVoltItem;
import com.htthorizon.powersupply.dto.VoltTgItem;
import com.htthorizon.powersupply.search.VoltTgSearch;
import com.htthorizon.powersupply.service.RdsGridTgVoltDfService;
import com.htthorizon.powersupply.service.TgVoltMonitorService;
import com.htthorizon.powersupply.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 成片低电压监测
 */
@RestController
@RequestMapping("tgVoltMonitor")
public class TgVoltMonitorController {

    @Autowired
    private TgVoltMonitorService tgVoltMonitorService;
    @Autowired
    private RdsGridTgVoltDfService rdsGridTgVoltDfService;

    @GetMapping("/getNewCol")
    public List<Map<String, Object>> getNewCol() {
        return rdsGridTgVoltDfService.getNewCol();
    }

    @GetMapping("/getRealTime")
    public R<List<TgLowVoltItem>> getLoad() {
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        Integer maxTime = tgVoltMonitorService.getMaxTime(dateDt);
        List<TgLowVoltItem> list = tgVoltMonitorService.getStatistics("now", maxTime, dateDt);
        return R.ok(list);
    }

    @GetMapping("/getToday")
    public R<List<TgLowVoltItem>> getToday() {
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        List<TgLowVoltItem> list = tgVoltMonitorService.getStatistics("today", null, dateDt);
        return R.ok(list);
    }

    //
    @GetMapping("/getAll")
    public R<List<TgLowVoltItem>> getAll() {
        List<TgLowVoltItem> list = tgVoltMonitorService.getStatistics("all", null, null);
        return R.ok(list);
    }

    @GetMapping("/getMaxTime")
    public Integer getMaxTime() {
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        return tgVoltMonitorService.getMaxTime(dateDt);
    }

    @GetMapping("/getVoltTgItems")
    public R<List<VoltTgItem>> getVoltTgItems(VoltTgSearch search) {
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        Integer maxTime = tgVoltMonitorService.getMaxTime(dateDt);
        if (StringUtils.isEmpty(search.getType()) || "now".equals(search.getType())) {
            search.setType("now");
            List<VoltTgItem> items = tgVoltMonitorService.getVoltTgItems(search, search.getType(), maxTime, dateDt);
            return R.ok(items);
        } else if ("today".equals(search.getType())) {
            List<VoltTgItem> items = tgVoltMonitorService.getVoltTgItems(search, "today", null, dateDt);
            return R.ok(items);
        } else {
            List<VoltTgItem> items = tgVoltMonitorService.getVoltTgItems(search, "all", null, null);
            return R.ok(items);
        }
        //items 获取： tg_nm
    }
}
